<template>
	<view>
		<Navbar></Navbar>
		<view class="u-m-25">
			<view class="u-form-box u-p-20 u-flex u-row-between u-col-top" v-if="defaultAddress.id">
				<view class="u-m-t-5">
					<u-icon name="map" size="38"></u-icon>
				</view>
				<view class="u-flex-1 u-m-l-10">
					<view class="u-text-line-15">
						<span>当前配送至：</span>
						<span>{{defaultAddress.address}}</span>
					</view>
					<view class="u-m-t-10">
						<span class="u-m-r-10">{{ defaultAddress.name }}</span>
						<span>{{defaultAddress.phone}}</span>
					</view>
				</view>
			</view>
			<view class="u-m-t-30 u-form-box u-p-t-30 u-p-b-10">
				<view class="u-font-bold u-font-34 u-m-b-20">收货地址</view>
				<view class="address_item" :class="index + 1 == addressList.length ? '' : 'line_bottom'" v-for="(item,index) in addressList" :key="item.id" @click="onSettingDefault(item)">
					<view class="u-flex-1">
						<view>{{item.address}}</view>
						<view class="u-m-t-10">
							<span class="u-m-r-10">{{ item.name }}</span>
							<span>{{item.phone}}</span>
						</view>
					</view>
					<view class="u-m-l-20" @click.native.stop="onEditAddress(item.id)">
						<u-icon name="edit-pen" size="40"></u-icon>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<view class="bottom_box" style="height: auto;">
			<view class="u-width-100">
				<view class="return common_css u-m-b-25" @click="onCreatWechatAddress()">一键获取地址</view>
				<view class="confirm common_css" @click="onSwitchAddAddress()">新增收货地址</view>
			</view>
		</view>
		
		<Modal :show="modal.show" :confirmText="modal.confirm" :cancelText="modal.cancel" :modalContent="modal.content" :showCancel="modal.showCancel"
			@confirm="modalConfirm" @cancel="modal.show = false"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Modal from '@/components/Modal.vue'
	import { teaAdressList, teaAdressAdd, teaAdressEdit } from '@/network/tea.js'
	export default {
		components: {
			Navbar,
			Modal
		},
		data() {
			return {
				addressList: [],
				defaultAddress: {},
				modal: {
					show: false,
					showCancel: true,
					confirm: '确定',
					cancel: '取消',
					content: '',
					data: {}
				},
				isPoint: false
			}
		},
		mounted(){
			let _this = this
			uni.$on('update',function(){
				_this.addressList = []
				_this.onGetAddressList()
			})
		},
		onLoad(options){
			if(options.isPoint){
				this.isPoint = options.isPoint
			}
			this.onGetAddressList()
		},
		onUnload() {
			uni.$off('update')
			this.stickyStyle = ''
		},
		methods:{
			async onGetAddressList(){
				const res = await teaAdressList({user_id: uni.getStorageSync("user_id")})
				this.addressList = res.data.list
				res.data.list.map(item=>{
					if(item.default == 1){
						this.defaultAddress = item
					}
				})
			},
			onSwitchAddAddress(){
				uni.navigateTo({
					url: "/teaBureau/teaAddressForm/teaAddressForm"
				})
			},
			onEditAddress(id){
				uni.navigateTo({
					url: "/teaBureau/teaAddressForm/teaAddressForm?id=" + id
				})
			},
			onSettingDefault(val){
				this.modal.data = val
				this.modal.content = '是否设置该地址为默认地址'
				this.modal.show = true
			},
			async modalConfirm(){
				const params = {
					address_id: this.modal.data.id,
					default: 1,
					name: this.modal.data.name,
					phone: this.modal.data.phone,
					door: this.modal.data.door,
					address: this.modal.data.address
				}
				const res = await teaAdressEdit(params)
				this.modal.show = false
				if(this.isPoint){
					uni.navigateBack({
						delta: 1
					})
				}else{
					this.addressList = []
					this.onGetAddressList()
				}
			},
			onCreatWechatAddress(){
				const _this = this
				uni.chooseAddress({
					success: async function(res){
						const params = {
							user_id: uni.getStorageSync("user_id"),
							name: res.userName,
							phone: res.telNumber,
							address: `${res.provinceName}${res.cityName}${res.countyName}${res.detailInfo}`,
							door: res.detailInfo,
							default: 1
						}
						await teaAdressAdd(params)
						_this.addressList = []
						_this.onGetAddressList()
					}
				})
			}
		}
	}
</script>

<style scoped>
	.address_item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 20rpx;
		margin-top: 20rpx;
	}
	
	.line_bottom{
		border-bottom: 1rpx solid #ccc;
	}
	
	.common_css{
		color:#fff;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 26rpx;
	}
	
	.return{
		height: 80rpx;
		background: #000;
		border-radius: 50rpx;
	}
	
	.confirm{
		height: 80rpx;
		background: #fb6316;
		border-radius: 50rpx;
	}
</style>